package com.example.search.controller.v1;

import com.example.model.common.dtos.ResponseResult;
import com.example.model.common.enums.AppHttpCodeEnum;
import com.example.model.search.dtos.HistorySearchDto;
import com.example.search.service.ApUserSearchService;
import com.example.utils.common.AppJwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/history")
@Slf4j
public class ApUserSearchController {

    @Autowired
    private ApUserSearchService apUserSearchService;

    @PostMapping("/load")
    public ResponseResult findUserSearch(HttpServletRequest request){
        String token = request.getHeader("ap_token");
        Jws<Claims> jws = AppJwtUtil.getJws(token);
        Claims claims = jws.getBody();
        String userId = claims.get("id").toString();
        log.info("登录用户id为: {}",userId);

//        if(userId == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_ADMIND);
//        }
        return apUserSearchService.findUserSearch(userId);
    }

    @PostMapping("/del/{id}")
    public ResponseResult delUserSearch(@PathVariable String id, HttpServletRequest request){
        System.out.println("hhh");
        String token = request.getHeader("ap_token");
        Jws<Claims> jws = AppJwtUtil.getJws(token);
        Claims claims = jws.getBody();
        Integer userId = Integer.parseInt(claims.get("id").toString());
        return apUserSearchService.delUserSearch(id,userId);
//        return null;
    }

}
